#ifndef _SDK_PROTO_H_
#define _SDK_PROTO_H_

#include "sdk_common.h"


#define VMAD0    "D0"       //attr get
#define VMAD1    "D1"       //attr set
#define VMAD2    "D2"       //attr set unackhnowledged
#define VMAD3    "D3"       //attr status
#define VMAD4    "D4"       //attr indcation
#define VMAD5    "D5"       //attr comfirmation

#define VMAD0HEX    0xD0       //attr get
#define VMAD1HEX    0xD1       //attr set
#define VMAD2HEX    0xD2       //attr set unackhnowledged
#define VMAD3HEX    0xD3       //attr status
#define VMAD4HEX    0xD4       //attr indcation
#define VMAD5HEX    0xD5       //attr comfirmation



#define ATTRID1007          "1007"
#define ATTRID1021          "1021"
#define ATTRID1008          "1008"
#define ATTRID1007HEX       0x1007
#define ATTRID1021HEX       0x1021
#define ATTRID1008HEX       0x1008



#define SDK_AUTH_REQUEST_FORMAT "{\"call\":\"ai.argrace.gw.auth\",\"pk\":\"%s\",\"sc\":\"%s\",\"id\":\"%s\",\"mac\":\"%s\",\"ak\":\"%s\"}"
#define SDK_UNREGIST_REQUEST_FORMAT "{\"call\":\"ai.argrace.gw.unregist\",\"pk\":\"%s\",\"sc\":\"%s\",\"id\":\"%s\",\"mac\":\"%s\",\"ak\":\"%s\"}"
#define SDK_3RD_INDCATE_HELLO_FORMAT  "{\"call\":\"ai.argrace.gw.hello\",\"did\":\"%s\",\"ak\":\"%s\",\"type\":\"gw\",\"ad\":\"%s\",\"extdata\":\"%s\"}"
#define SDK_3RD_INDCATE_BYE_FORMAT    "{\"call\":\"ai.argrace.gw.bye\",\"did\":\"%s\",\"ak\":\"%s\",\"type\":\"gw\",\"ad\":\"%s\",\"extdata\":\"%s\"}"
#define SDK_3RD_CTL_FORMAT "{\"call\":\"ai.argrace.gw.data.set\",\"ak\":\"%s\",\"gwid\":%u,\"ad\":\"%s\",\"op\":\"xx\",\"vma\":\"%s\",\"data\":\"%s\",\"tid\":\"%s\",\"mac\":\"%s\"}"


typedef enum{
    enum_sdk_dt_outlet=1,	//插座	
    enum_sdk_dt_switchs=2,	//开关	
    enum_sdk_dt_cutain=3,	//窗帘开关	
    enum_sdk_dt_sence=4,	//场景开关	
    enum_sdk_dt_bultlight=5,	//灯泡	
    enum_sdk_dt_branchlight=6,	//灯带	
    enum_sdk_dt_temphu_sensor=7,	//温湿度传感器	
    enum_sdk_dt_door_sensor=8,	//门磁	
    enum_sdk_dt_body_sensor=9,	//人体传感器	
    enum_sdk_dt_water_sensor=10,	//浸水传感器
    enum_sdk_dt_gas_alarm_sensor=11,	//燃气报警器	
    enum_sdk_dt_smoke_sensor=12,	//烟雾报警器	
    enum_sdk_dt_homedoor_locker=13,	//家用门锁	
    enum_sdk_dt_skin_machine=14,	//皮肤仪	
    enum_sdk_dt_dimming=15,	//调光开关	
    enum_sdk_dt_wire_gateway=16,	//Test1 有线网关
    enum_sdk_dt_On_switch=17,	//通断器	
    enum_sdk_dt_sleep_monitor=18,	//睡眠监测仪	
    enum_sdk_dt_falldown_sensor=19,	//跌倒检测仪
    enum_sdk_dt_tv=20,	//电视
    enum_sdk_dt_smart_ccup=21,	//智能水杯	
    enum_sdk_dt_skateboard=22,	//滑板车	
    enum_sdk_dt_power_outlets=23,	//电源插座	
    enum_sdk_dt_sensor_lights=24,	//感应灯	
    enum_sdk_dt_ceiling_light=25,	//吸顶灯	
    enum_sdk_dt_heating_lamps=26,	//取暖灯	
    enum_sdk_dt_fanlight=27,	//风扇灯	
    enum_sdk_dt_down_light=28,	//筒灯	
    enum_sdk_dt_spot_light=29,	//射灯	
    enum_sdk_dt_wireless_switchs=30,	//无线开关
    enum_sdk_dt_light_controller=31,	//照明遥控器	
    enum_sdk_dt_track_maginetic_light=32,	//轨道磁吸灯	
    enum_sdk_dt_air_condintioner=33,	//空调	
    enum_sdk_dt_curtain_motor=34,	//窗帘电机	
    enum_sdk_dt_env_sensor=35,	//环境传感器	
    enum_sdk_dt_pwm_complementaion=36,	//两线方案	
    enum_sdk_dt_maginetic_lamps=37,	//磁吸灯	
    enum_sdk_dt_av_entertainment=38,	//影音娱乐	
    enum_sdk_dt_cctv=39,	//摄像头	
    enum_sdk_dt_app=40,	//app
    enum_sdk_dt_bath_bombs=41,	//浴霸	
    enum_sdk_dt_ira_controller=42,	//红外遥控器	
    enum_sdk_dt_aircond_controller=43,	//空调遥控器	
    enum_sdk_dt_tv_controller=44,	//电视遥控器	
    enum_sdk_dt_constant_illumination_light=45,	//恒照灯	
    enum_sdk_dt_novation_product=46,	//创新产品	
    enum_sdk_dt_misc=47,	//其他	
    enum_sdk_dt_temp_contoller=48,	//温控器	
    enum_sdk_dt_wireless_gateway=49,	//无线网关	
    enum_sdk_dt_waterHeater=50	//热水器
}ENUM_SDK_DEVICE_TYPE;


typedef enum{
    enum_proto_mt_outlet = 1,
    enum_proto_mt_sw = 2,
    enum_proto_mt_cw =3,
    enum_proto_mt_hsb = 4,
    enum_proto_mt_aircond=5,
    enum_proto_mt_curtain=6,
    enum_proto_mt_max = 0xFF
}enum_proto_device_type_t;


struct device_proto_smart_outlet_aircond_t{
    uint8_t swonoff; //插座(继电器)开关 0关，1开
    uint8_t aircond_onoff;//空调开关 0关，1开
    uint8_t temp;//设置温度 （1~15代表16~30度）
    uint8_t overload; //过载警报 0正常，1过载
    uint8_t gear;//档位 0~5 
    uint8_t mode;//模式 1~10
    uint32_t consumption;//累计功耗 单位：100mWH（0.1瓦时）
    uint8_t wind_direction_updown;//上下扫风状态 0关，1开
    uint8_t wind_direction_leftright;//左右扫风状态 0关，1开
    uint8_t currenct;//电流 单位mA
    uint16_t power;//功率 单位: 100mW（0.1瓦） 范围: [0w，6553.5w]
    uint8_t voltage;//电压 单位: V  范围：[20v~275v]计算公式：电压 = value + 20 
};

struct device_proto_light_cw_t{
    uint8_t onoff;
    uint8_t lnss;
    uint8_t colortemp;
};
struct device_proto_light_color_t{
    uint8_t onoff;//
    uint16_t hue;//
    uint8_t saturation;
    uint8_t brightness;//
};
struct device_proto_switch_t{
    uint8_t switchidx[4];//
};
struct device_proto_curtain_t{
    uint8_t motorOnOff;//电机状态
    uint8_t direction;//方向设置
    uint8_t handdrag;//手拉启动设置
    uint8_t motorPosition;//电机位置控制
    uint8_t angleDirection;//角度方向
    uint8_t andglePercentage;//角度百分比
};
struct device_proto_CAC_t{
    uint8_t devType; //新风/地暖/空调外机/空调内机
    uint8_t onoff;//0-off 1-on
    uint8_t tempInRoom;//温度(室温) 1~15代表16~30度
    uint8_t windspeed;//0-5,0-自动（内机）  1-高速 2-中速 3-中高速 4-低速 5-中低速
    //空调机模式:1-10,0-无效 1-设定制冷 2-设定除湿 3-设定清爽 4-设定送风 5-设定自动除湿 6-设定贴心睡眠 8-设定制热 9-设定地暖 10-设定强热（地暖和制热同时开启）
    uint8_t mode;
    uint8_t error;//0-no error
    uint8_t onoffline;//1-online 0-offline
    //---------新风/地暖/空调外机-----------
    uint8_t temp;//1~15代表16~30度 （用于地暖设备上报）
    uint8_t antifreeze_winddirection;//0-off 1-on    - 1 小角度 2 中角度 4 大角度 7 摆动
    //--------------空调内机-----------
    uint8_t aircondtemp;//设置温度（1~15代表16~30度）
    /*
        内机风向：
        0 无导风板（上报）
        1 小角度
        2 中角度
        4 大角度
        7 摆动   
    */
    uint8_t direction;//风向
};

typedef union device_data_t {
    struct device_proto_light_cw_t cw;
    struct device_proto_light_color_t color;
    struct device_proto_switch_t sw;
    struct device_proto_curtain_t curtain;
    struct device_proto_CAC_t aircond;
    struct device_proto_smart_outlet_aircond_t outlet;
}O_DEVICE_DATA,*P_DEVICE_DATA;




typedef void (*Fp_HeartBeat_Cb)(int);
typedef void (* pFuncDeviceOnoffline_t)(unsigned short addr,unsigned char onoffline);
typedef int (* pFuncDeviceExtdata_t)(unsigned short addr,const char *extdata,unsigned short extdataLen);
typedef void (* pFuncDevicedata_t)(unsigned short addr,const int dtype,void *pdata);

typedef struct{
    uint8_t type;
    uint8_t random[2];
    uint16_t payload_len;
    uint8_t *p_payload;
    uint8_t chksum[2];
}O_3RD_PAYLOAD,*P_3RD_PAYLOAD;

typedef struct{
    uint8_t devid[35];
    uint8_t productKey[15];
    uint8_t deviceSecret[35];
    char targetIp[16];
    uint16_t auth_port;
    char decsUrl[200];
    char authOrig[512];
    char accesskey[200];
    uint16_t gwid;
    uint16_t port;
    char ip[16];
    char mac[13];
}O_SDK_CONTEXT,*P_SDK_CONTEXT;

extern O_SDK_CONTEXT sdk_context;
extern uint8_t * auth_buffer;
extern int auth_buffer_len;
extern uint8_t * data_buffer;
extern int data_buffer_len;


extern int sdk_proto_auth(uint8_t retry);
extern int sdk_proto_unregist(uint8_t retry);
extern int sdk_proto_get_descfile(char *buffer,int len);
extern int sdk_proto_ctl_send(uint8_t *pdata,uint16_t len);
extern int sdk_proto_auth_send(uint8_t *pdata,uint16_t len,uint8_t retry);
extern int sdk_proto_cloud_report(uint16_t addr, uint8_t * msg,uint16_t msglen);
extern P_SDK_CONTEXT sdk_proto_context_get(void);
extern void sdk_proto_context_clear(void);
extern int sdk_proto_auth_socket_init(void);
extern uint8_t sdk_proto_get_tid(void);
extern uint8_t sdk_proto_send_retry_set(uint8_t retry);
extern uint8_t sdk_proto_send_retry_get(void);
extern void sdk_proto_license_load(uint8_t devid[35],uint8_t productKey[15],uint8_t deviceSecret[35]);
extern int sdk_proto_auth_recv(uint8_t *buf,long unsigned int len);
extern int sdk_proto_data_recv(uint8_t *buf,long unsigned int len);
extern int sdk_proto_target_set(char *ip);
extern int sdk_proto_target_set_authport(unsigned short port);
extern int sdk_proto_target_set_dataport(unsigned short port);
extern int sdk_proto_auth_recv_thread_init(void);
extern int sdk_proto_hello_send(Fp_HeartBeat_Cb cb,int code);



extern void sdk_proto_device_onoffline_funcb_regist(pFuncDeviceOnoffline_t pfunc);
extern void sdk_proto_device_extdata_funcb_regist(pFuncDeviceExtdata_t pfunc);
extern void sdk_proto_device_data_funcb_regist(pFuncDevicedata_t pfunc);



#endif
